// <define:import.meta.env>
var define_import_meta_env_default = { DEV: false, PROD: true, SSR: true };

// src/renderContext.tsx
import {
  createContext,
  useContext,
  useRef,
  useLayoutEffect
} from "react";

// src/asyncLocalStorage.ts
var AsyncStorageForServerContext;
var getRenderContextFromAsyncLocalStorage = () => {
  if (!(define_import_meta_env_default == null ? void 0 : define_import_meta_env_default.SSR)) {
    throw new Error(`Cannot use server context from browser`);
  }
  return AsyncStorageForServerContext ? AsyncStorageForServerContext.getStore() : void 0;
};

// src/renderContext.tsx
import { jsx } from "react/jsx-runtime";
var renderSequenceNum = 1;
var createNewServerRequestMap = (renderId) => {
  if (import.meta && define_import_meta_env_default && define_import_meta_env_default.DEBUG) {
    return /* @__PURE__ */ new Map([["__tempRenderId", renderId]]);
  }
  return /* @__PURE__ */ new Map();
};
var createServerRenderContextV2 = (pageUrl, {
  environmentConfig,
  entryPointBasePath,
  sharedServerRequestData,
  supplementalFieldValues,
  messagesForBrowser = [],
  resolvedDataDependencies
}) => {
  const renderId = (renderSequenceNum++).toString(16);
  const perServerRequestData = sharedServerRequestData ?? createNewServerRequestMap(renderId);
  return {
    pageUrl,
    environmentConfig,
    islands: [],
    inlineAssets: [],
    renderId,
    perServerRequestData,
    messagesForBrowser,
    entryPointBasePath,
    supplementalFieldValues,
    criticalDevErrors: [],
    modules: {},
    contentDescriptors: {
      modules: {},
      dndAreas: {}
    },
    resolvedDataDependencies
  };
};
var createEmptyServerRenderContext = () => createServerRenderContextV2(null, { environmentConfig: null });
var ServerRenderContext = createContext(
  createEmptyServerRenderContext()
);
var useServerRenderContext = () => {
  if (!(define_import_meta_env_default == null ? void 0 : define_import_meta_env_default.SSR)) {
    throw new Error(`Cannot use server context from browser`);
  }
  const contextValueFromAls = getRenderContextFromAsyncLocalStorage();
  if (contextValueFromAls) {
    return contextValueFromAls;
  }
  return useContext(ServerRenderContext);
};
var createClientRenderContext = (hsServerPageUrl, hsBasePath, islands = []) => {
  return {
    pageUrl: hsServerPageUrl,
    islands,
    island: null,
    hydrationFinished: { current: false },
    entryPointBasePath: hsBasePath
  };
};
var ClientRenderContext = createContext(
  createClientRenderContext(null, null)
);

// src/config/singleton.ts
var hsConfigKey = typeof Symbol !== "undefined" ? Symbol.for("hs-config-singleton") : "HS_CONFIG_SINGLETON";
function assertConfigSingletonDefined() {
  if (!window[hsConfigKey]) {
    throw new Error("No environment config singleton has been setup");
  }
}
function getConfigSingleton() {
  if (define_import_meta_env_default.SSR) {
    throw new Error(
      "Config singletons only work in the browser, not server-side"
    );
  }
  assertConfigSingletonDefined();
  return window[hsConfigKey];
}

// src/getters/getEnvironmentConfig.ts
function getEnvironmentConfig() {
  if (define_import_meta_env_default.SSR) {
    return useServerRenderContext().environmentConfig;
  } else {
    return getConfigSingleton();
  }
}

// src/internal/getters.ts
var getInternalHSEnv = () => {
  return getEnvironmentConfig().getInternalHSEnv();
};
var getInternalRenderSource = () => {
  return getEnvironmentConfig().getInternalRenderSource();
};

// src/lib/cacheControl.ts
var HEADER_REGEXP = /([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?/g;
var STRINGS = {
  maxAge: "max-age",
  sharedMaxAge: "s-maxage",
  maxStale: "max-stale",
  minFresh: "min-fresh",
  immutable: "immutable",
  mustRevalidate: "must-revalidate",
  noCache: "no-cache",
  noStore: "no-store",
  noTransform: "no-transform",
  onlyIfCached: "only-if-cached",
  private: "private",
  proxyRevalidate: "proxy-revalidate",
  public: "public",
  staleWhileRevalidate: "stale-while-revalidate",
  staleIfError: "stale-if-error"
};
function parseBooleanOnly(value) {
  return value === null;
}
function parseDuration(value) {
  if (!value) {
    return null;
  }
  const duration = Number.parseInt(value, 10);
  if (!Number.isFinite(duration) || duration < 0) {
    return null;
  }
  return duration;
}
var CacheControl = class {
  maxAge;
  sharedMaxAge;
  maxStale;
  maxStaleDuration;
  minFresh;
  immutable;
  mustRevalidate;
  noCache;
  noStore;
  noTransform;
  onlyIfCached;
  private;
  proxyRevalidate;
  public;
  staleWhileRevalidate;
  staleIfError;
  constructor() {
    this.maxAge = null;
    this.sharedMaxAge = null;
    this.maxStale = null;
    this.maxStaleDuration = null;
    this.minFresh = null;
    this.immutable = null;
    this.mustRevalidate = null;
    this.noCache = null;
    this.noStore = null;
    this.noTransform = null;
    this.onlyIfCached = null;
    this.private = null;
    this.proxyRevalidate = null;
    this.public = null;
    this.staleWhileRevalidate = null;
    this.staleIfError = null;
  }
  parse(header) {
    if (!header || header.length === 0) {
      return this;
    }
    const values = {};
    const matches = header.match(HEADER_REGEXP) ?? [];
    for (const match of matches) {
      const tokens = match.split("=", 2);
      const [key] = tokens;
      values[key.toLowerCase()] = tokens.length > 1 ? tokens[1].trim() : null;
    }
    this.maxAge = parseDuration(values[STRINGS.maxAge]);
    this.sharedMaxAge = parseDuration(values[STRINGS.sharedMaxAge]);
    this.maxStale = parseBooleanOnly(values[STRINGS.maxStale]);
    this.maxStaleDuration = parseDuration(values[STRINGS.maxStale]);
    if (this.maxStaleDuration) {
      this.maxStale = true;
    }
    this.minFresh = parseDuration(values[STRINGS.minFresh]);
    this.immutable = parseBooleanOnly(values[STRINGS.immutable]);
    this.mustRevalidate = parseBooleanOnly(values[STRINGS.mustRevalidate]);
    this.noCache = parseBooleanOnly(values[STRINGS.noCache]);
    this.noStore = parseBooleanOnly(values[STRINGS.noStore]);
    this.noTransform = parseBooleanOnly(values[STRINGS.noTransform]);
    this.onlyIfCached = parseBooleanOnly(values[STRINGS.onlyIfCached]);
    this.private = parseBooleanOnly(values[STRINGS.private]);
    this.proxyRevalidate = parseBooleanOnly(values[STRINGS.proxyRevalidate]);
    this.public = parseBooleanOnly(values[STRINGS.public]);
    this.staleWhileRevalidate = parseDuration(
      values[STRINGS.staleWhileRevalidate]
    );
    this.staleIfError = parseDuration(values[STRINGS.staleIfError]);
    return this;
  }
  format() {
    const tokens = [];
    if (typeof this.maxAge === "number") {
      tokens.push(`${STRINGS.maxAge}=${this.maxAge}`);
    }
    if (typeof this.sharedMaxAge === "number") {
      tokens.push(`${STRINGS.sharedMaxAge}=${this.sharedMaxAge}`);
    }
    if (this.maxStale) {
      if (typeof this.maxStaleDuration === "number") {
        tokens.push(`${STRINGS.maxStale}=${this.maxStaleDuration}`);
      } else {
        tokens.push(STRINGS.maxStale);
      }
    }
    if (typeof this.minFresh === "number") {
      tokens.push(`${STRINGS.minFresh}=${this.minFresh}`);
    }
    if (this.immutable) {
      tokens.push(STRINGS.immutable);
    }
    if (this.mustRevalidate) {
      tokens.push(STRINGS.mustRevalidate);
    }
    if (this.noCache) {
      tokens.push(STRINGS.noCache);
    }
    if (this.noStore) {
      tokens.push(STRINGS.noStore);
    }
    if (this.noTransform) {
      tokens.push(STRINGS.noTransform);
    }
    if (this.onlyIfCached) {
      tokens.push(STRINGS.onlyIfCached);
    }
    if (this.private) {
      tokens.push(STRINGS.private);
    }
    if (this.proxyRevalidate) {
      tokens.push(STRINGS.proxyRevalidate);
    }
    if (this.public) {
      tokens.push(STRINGS.public);
    }
    if (typeof this.staleWhileRevalidate === "number") {
      tokens.push(
        `${STRINGS.staleWhileRevalidate}=${this.staleWhileRevalidate}`
      );
    }
    if (typeof this.staleIfError === "number") {
      tokens.push(`${STRINGS.staleIfError}=${this.staleIfError}`);
    }
    return tokens.join(", ");
  }
};
function format(cc) {
  if (!(cc instanceof CacheControl)) {
    return CacheControl.prototype.format.call(cc);
  }
  return cc.format();
}

// src/lib/mapCachingObject.ts
var DEFAULT_CACHE_AGE = 10;
var mapCachingObject = (cacheConfig = {}) => {
  const { cacheControl, eTag, lastModified } = cacheConfig;
  const cachingHeaders = {
    ["Cache-Control"]: format({
      maxAge: DEFAULT_CACHE_AGE,
      sharedMaxAge: DEFAULT_CACHE_AGE
    })
  };
  if (cacheControl) {
    cachingHeaders["Cache-Control"] = format(cacheControl);
  }
  if (eTag && typeof eTag === "string") {
    cachingHeaders["ETag"] = eTag;
  }
  if (lastModified) {
    let lastModifiedString;
    if (lastModified instanceof Date) {
      lastModifiedString = lastModified.toUTCString();
    }
    if (typeof lastModified === "string") {
      const isValidDateString = !isNaN(Date.parse(lastModified));
      if (isValidDateString) {
        lastModifiedString = lastModified;
      }
    }
    if (typeof lastModified === "number") {
      const isValidDateNumber = !isNaN(new Date(lastModified).getTime());
      if (isValidDateNumber) {
        lastModifiedString = new Date(lastModified).toUTCString();
      }
    }
    if (lastModifiedString) {
      cachingHeaders["Last-Modified"] = lastModifiedString;
    }
  }
  return cachingHeaders;
};
var mapCachingObject_default = mapCachingObject;

// src/components/DangerousHtmlNoWrapper.tsx
import { useId } from "react";
import { jsx as jsx2 } from "react/jsx-runtime";
function DangerousHtmlNoWrapper({ __html }) {
  const id = useId();
  const htmlWithTemplatesAround = `</template>${__html}<template data-hs-remove="${id}">`;
  if (!define_import_meta_env_default.SSR) {
    throw new Error("DangerousHtmlNoWrapper must only be used on the server");
  } else if (__html == null) {
    throw new Error(
      "DangerousHtmlNoWrapper must have a valid __html string prop"
    );
  }
  return /* @__PURE__ */ jsx2(
    "template",
    {
      "data-hs-remove": id,
      dangerouslySetInnerHTML: { __html: htmlWithTemplatesAround }
    }
  );
}
function removeAllDangerousHtmlSiblingTemplateElementHtml(html) {
  return html.replace(
    /<template data-hs-remove="[:\d\w-.]+"><\/template>/g,
    ""
  );
}
export {
  DangerousHtmlNoWrapper,
  getInternalHSEnv,
  getInternalRenderSource,
  mapCachingObject_default as mapCachingObject,
  removeAllDangerousHtmlSiblingTemplateElementHtml
};
